import logging

import allure
import pytest

from config.config import *
from api_keyword.api_key import ApiKey

# cookie
@pytest.fixture(scope='session')
def cookie_fix():
    ak = ApiKey()
    data = {
        "username": "admin",
        "pswd": "65a0ec385ca6a0c1e20d1f8270c28303",
        "rememberme": "0"
    }
    headers = {
        "Content-Type": "application/x-www-form-urlencoded"
    }
    url = 'common/login'
    res = ak.post(url=url, headers=headers, data=data)
    set_cookie = res.headers['Set-Cookie']
    return set_cookie


# 打印日志
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item,call):
    out=yield
    res =out.get_result()
    if res.when == "call":
        logging.info(f'测试编号{res.nodeid}')
        logging.info(f'测试步骤{res.outcome}')
        logging.info(f'故障详情{res.longrepr}')
        logging.info(f'异常{call.excinfo}')
        logging.info(f'用例耗时{res.duration}')
        if res.failed:
            with allure.step('添加失败截图...'):
                # 这里可以添加截图逻辑，示例中简单打印信息
                allure.attach('失败截图信息', '失败截图', allure.attachment_type.PNG)